// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store'

const requireAuth = (to, from, next) => {
  const userStore = useUserStore()
  const userInfo = userStore.userInfo
  if (userInfo) {
    next()
  } else {
    next('/login')
  }
}

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: {
      title: '视易商城-流媒体合租平台',
      content: {
        keywords: '视易商城-流媒体合租平台',
        description: '视易商城-流媒体合租平台'
      }
    },
    component: () => import('@/views/index.vue')
  },
  {
    path: '/login',
    name: '登录',
    meta: {
      // 需要隐藏 Header 和 Footer 组件
      hideHeaderFooter: true,
      title: '视易商城-登录',
      content: {
        keywords: '视易商城-登录',
        description: '视易商城-登录'
      }
    },
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/about',
    name: '关于',
    meta: {
      title: '视易商城-关于',
      content: {
        keywords: '视易商城-关于',
        description: '视易商城-关于'
      }
    },
    component: () => import('@/views/about/index.vue')
  },
  {
    path: '/promotion',
    name: '推广',
    meta: {
      title: '视易商城-推广',
      content: {
        keywords: '视易商城-推广',
        description: '视易商城-推广'
      }
    },
    component: () => import('@/views/promotion/index.vue')
  },
  {
    path: '/info',
    name: '个人',
    meta: {
      title: '视易商城-个人',
      content: {
        keywords: '视易商城-个人',
        description: '视易商城-个人'
      }
    },
    component: () => import('@/views/userInfo/index.vue'),
    beforeEnter: requireAuth
  },
  {
    path: '/order',
    name: '订单',
    meta: {
      title: '视易商城-订单',
      content: {
        keywords: '视易商城-订单',
        description: '视易商城-订单'
      }
    },
    component: () => import('@/views/order/index.vue'),
    beforeEnter: requireAuth
  },

  {
    path: '/help',
    name: 'help',
    meta: {
      title: '视易商城-帮助',
      content: {
        keywords: '视易商城-帮助',
        description: '视易商城-帮助'
      }
    },
    component: () => import('../views/help/index.vue')
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import('../views/details/index.vue')
  },
  {
    path: '/follow',
    name: 'follow',
    component: () => import('../views/follow/index.vue')
  },
  {
    path: '/:catchAll(.*)',
    name: 'NotFound',
    meta: {
      title: '404 - 页面不存在',
      content: {
        keywords: '视易商城-页面不存在',
        description: '404 - 页面不存在'
      }
    },
    component: () => import('@/views/notFound/index.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router
